package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.leyou.common.constant.PageResult;
import com.leyou.common.exception.domain.ExceptionEnum;
import com.leyou.common.exception.domain.LyException;

import com.leyou.common.utils.BeanHelper;

import com.leyou.item.domian.Brand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> brandPageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //添加分页条件
        PageHelper.startPage(page, rows);
        //封装查询条件，包括排序
        Example example = new Example(Brand.class);
        //判断查询条件是否有值，有就封装条件
        if(!StringUtils.isBlank(key)){
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name", "%"+key.toUpperCase()+"%");
            criteria.orEqualTo("letter", key.toUpperCase());
        }
        //判断是否有排序字段   select * from tb_brand where name=xx order by name desc
        if(!StringUtils.isBlank(sortBy)){
            example.setOrderByClause(sortBy+" "+(desc?"desc":"asc"));
        }
        //mybatis查询操作
        List<Brand> brands = brandMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //得到PageHelper的分页对象
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        //封装自定义的分页对象
        List<Brand> list = pageInfo.getList();
        PageResult<BrandDTO> pageResult = new PageResult<>(pageInfo.getTotal(),
                BeanHelper.copyWithCollection(list, BrandDTO.class));
        return pageResult;
    }

    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        try {
            //将dto转成pojo
            Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
            //保存品牌 insertSelective方法会在生成sql语句的时候自动去掉对象中的null。
            brandMapper.insertSelective(brand);
            //获取到新添加的品牌对象的id
            Long brandId = brand.getId();
            //保存中间表
            brandMapper.insertBrandAndCategory(brandId, cids);
        }catch (Exception e){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 根据品牌id查询品牌对象
     * @param id
     * @return
     */
    public BrandDTO findBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }

    /**
     * //根据分类id查询品牌列表
     * @param id
     * @return
     */
    public List<BrandDTO> findBrandByCid(Long id) {
        List<Brand> list = brandMapper.findBrandsByCid(id);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);

        }
        return BeanHelper.copyWithCollection(list,BrandDTO.class);

    }
}
